package com.chinaoly.baotou.web.controller;

import com.chinaoly.frm.common.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description class
 * Create by sunlm
 * on 2023-01-16 16:42
 * Make small but daily progress
 */
@Slf4j
@RestController
@RequestMapping(value = "/es")
@Api(tags = "公安网从华为云ES7查询动态数据给zhsq服务调用接口")
public class EsController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("/search")
    @ApiOperation(value = "es查询",notes = "查询ES中的数据")
    public String es(@RequestParam String matchPhraseQuery,@RequestParam String startTime,@RequestParam String endTime,@RequestParam String matchQuery,@RequestParam String index) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        int page = 1;
        int rows = 10;
        // 分页采用简单的from + size分页，适用数据量小的，了解更多分页方式可自行查阅资料
        searchSourceBuilder.from((page - 1) * rows);
        searchSourceBuilder.size(rows);

        // 对中文精确匹配
//        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//        if (!CommonUtil.isNull(matchPhraseQuery)) {
//            boolBuilder.filter(QueryBuilders.matchPhraseQuery("gjKey", matchPhraseQuery));
//        }

        // 时间范围查询 2022-08-12 14:50:41
//        RangeQueryBuilder rangeQueryBuilder = null;
//        if (!CommonUtil.isNull(startTime) || !CommonUtil.isNull(endTime)) {
//            rangeQueryBuilder = QueryBuilders.rangeQuery("createTime");
//            if (!CommonUtil.isNull(startTime)) {
//                rangeQueryBuilder.gte(startTime);
//            }
//            if (!CommonUtil.isNull(endTime)) {
//                rangeQueryBuilder.lte(endTime);
//            }
//        }

//        boolBuilder.filter(QueryBuilders.matchQuery("gjKey", matchQuery));

        // 范围查询参数不为空就加上
//        if (null != rangeQueryBuilder) {
//            boolBuilder.filter(rangeQueryBuilder);
//        }
//        searchSourceBuilder.query(boolBuilder);

        // 排序，根据时间倒叙
//        searchSourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));

        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.source(searchSourceBuilder);
        // 查询ES
        SearchHit[] searchHits = null;
        try {
            log.info("---------------------------------DSL查询语句为：{}", searchSourceBuilder.toString());
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            log.info("---------------------------------Search response is {}.", searchResponse.toString());
            SearchHits hits = searchResponse.getHits();
            // 获取总数
            // total = hits.getTotalHits().value;
            searchHits = hits.getHits();
            return  searchResponse.toString();
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
        }
        return "";
    }

}
